MyServiceInterface.java
package com.ivoronline.springboot_autowired_qualifier.services;
public interface MyServiceInterface {
public String sayHello();
}
MyServiceImplementation1.java
package com.ivoronline.springboot_autowired_qualifier.services;
import org.springframework.stereotype.Service;
@Service("impl1")
public class MyServiceImplementation1 implements MyServiceInterface {
public String sayHello() {
return "Hello";
}
}
MyServiceImplementation2.java
package com.ivoronline.springboot_autowired_qualifier.services;
import org.springframework.stereotype.Service;
@Service("impl2")
public class MyServiceImplementation2 implements MyServiceInterface {
public String sayHello() {
return "Hello World";
}
}
MyController.java
package com.ivoronline.springboot_autowired_qualifier.controllers;
import com.ivoronline.springboot_autowired_qualifier.services.MyServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
@Qualifier("impl1")
MyServiceInterface myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}